import { Component, OnInit } from '@angular/core';
import {Http} from "@angular/http";
import {MaintainService} from "../../shared/service/maintain.service";
import {MaintainRecord} from "../../shared/model/maintainRecord";
import {MaintainRecordSearch} from "../../shared/model/maintainRecordSearch";
declare var $: any;

@Component({
  selector: 'app-maintain-record-table',
  templateUrl: './maintain-record-table.component.html',
  styleUrls: ['./maintain-record-table.component.css']
})
export class MaintainRecordTableComponent implements OnInit {

  //选择人员分页
  _current = 1;
  _pageSize = 10;
  _total = 1;
  _dataSet = [];
  _loading = true;
  allCheckbox:boolean = false;
  queryId = "";
  queryName = "";
  type:string;
  vehicleoid:string = "";
  starttime:any = "";
  endtime:any = "";
  typeoid:string = "";
  status:number = -1;
  maintainRecordList:MaintainRecord[] = [];
  searchParam:MaintainRecordSearch = new MaintainRecordSearch; //高级查询条件

  //选择框
  _allChecked = false;
  _displayData: Array<any> = [];
  _checkedNumber = 0;
  _checkInfo= [];
  checkAllInfo = [];

  constructor(
    private http: Http,
    private maintainService:MaintainService
  ) { }

  ngOnInit() {

    //获取home父页面的值,用于高级查询
    if(this.searchParam.vehicle){
      this.vehicleoid = this.searchParam.vehicle;
      this.starttime = this.searchParam.starttime;
      this.endtime = this.searchParam.endtime;
      this.typeoid = this.searchParam.maintainType;
      this.status = this.searchParam.status;
    }
    //查询数据
    this.getDatas();
  }

  getDatas(){
    let start = "";
    if(this.starttime){
      start = this.starttime.substr(0,16);
    }
    let end = "";
    if(this.endtime){
      end = this.endtime.substr(0,16);
    }
    //选择车辆后查询数据
    if(this.vehicleoid){
      this.maintainService.getMaintainRecords(this._current,this._pageSize,this.vehicleoid,start,end,this.typeoid,this.status)
        .then(res=>{
          // console.log(res);
        this.maintainRecordList = res.list;
        this.allCheckbox = false;
        for(let i=0;i<this.maintainRecordList.length;i++){
          //判断维护记录状态
          let statusname = "";
          switch (this.maintainRecordList[i].status) {
            case 0:
              statusname = "维护中";
              break;
            case 1:
              statusname = "已结束";
              break;
          }
          this.maintainRecordList[i].statusname = statusname;
          //判断全选框是否显示
          if(this.maintainRecordList[i].status === 0&&this.maintainRecordList[i].status != 1){
            this.allCheckbox = true;
          }
        }

        this._dataSet = this.maintainRecordList;
        this._loading = false;
        this._total = res.total;
        this._displayData = this.maintainRecordList;
        // console.log(res);

        //查询选中
        for(var i=0;i<this._checkInfo.length;i++){
          if(this.checkAllInfo.indexOf(this._checkInfo[i].oid) == -1){
            this.checkAllInfo.push(this._checkInfo[i].oid);
          }
        }

        this.select(this.checkAllInfo,this._dataSet);
        this._refreshStatus("","");
      });
    }
  }

  refreshData(reset = false) {
    if (reset) {
      this._current = 1;
    }
    // this._allChecked = false;
    this._loading = true;
    this.getDatas();
  };

  _displayDataChange($event) {
    this._displayData = $event;
  };

  _checkAll(value) {
    if (value) {
      for(let i =0;i<this._displayData.length;i++){
        if(this._displayData[i].status===0&&this._displayData[i].status!= 1){
          this._displayData[i].checked = true;
        }
      }
    } else {
      this._displayData.forEach(data => data.checked = false);
      for(let i=0;i<this._displayData.length;i++){
        let numb = this.checkAllInfo.indexOf(this._displayData[i].oid);
        if(numb!=-1){
          this.checkAllInfo.splice(numb,1);
        }
      }
    }
    this._refreshStatus("","");
  }

  //当前页checkbox选中判断
  _refreshStatus(event,oid) {
    //全选判断
    if(this._displayData.length>0){
      let count:number = 0;
      let checkNumb:number = 0;
      for(let i =0;i<this._displayData.length;i++){
        if(this._displayData[i].status ===0&&this._displayData[i].status != 1){
          count++;
          if(this._displayData[i].checked == true){
            checkNumb++;
          }
        }
      }
      if(checkNumb!=0 && count==checkNumb){
        this._allChecked = true;
      }else{
        this._allChecked = false;
      }
    }
    this._checkedNumber = this._dataSet.filter(value => value.checked).length;
    this._checkInfo = this._dataSet.filter(value => value.checked);  //存放选中行的信息
    //判断是否有checkbox取消事件
    if(event==false){
      let numb = this.checkAllInfo.indexOf(oid);
      if(numb!=-1){
        this.checkAllInfo.splice(numb,1);
      }
    }
  }

  //选择判断
  select(selectedData:any,currentData:any){
    let selectInfo = [];
    if(selectedData){
      for(var i=0;i<currentData.length;i++){
        for(var j=0;j<selectedData.length;j++){
          if(currentData[i].oid == selectedData[j]){
            selectInfo.push(currentData[i]);
          }
        }
      }
      selectInfo.forEach(data => data.checked = true);
    }
  }

  // typeChange(){
  //   this.clear();
  //   this.type = $("#openwin_querytype").val() as string;
  //   this._current = 1;
  //   this.dataByType(this._current);
  // }
  //
  // //根据条件查找
  // search() {
  //   this.queryId = $('#searchId').val() as string;
  //   this.queryName = $('#searchName').val() as string;
  //   this._current = 1;
  //   this.dataByType(this._current);
  // }
  //
  // //类型数据判断
  // dataByType(currentPage:number){
  //
  // }

  // //清空查询条件
  // clear(){
  //   this.queryId = "";
  //   this.queryName = "";
  //   $("#searchId").val("");
  //   $("#searchName").val("");
  // }

  delInfo(){
    //将当前页所选会议信息插入所有选择信息变量checkAllInfo中
    for(var i=0;i<this._checkInfo.length;i++){
      if(this.checkAllInfo.indexOf(this._checkInfo[i].oid) == -1){
        this.checkAllInfo.push(this._checkInfo[i].oid);
      }
    }
    if(this.checkAllInfo.length>0){
      if(confirm("共选择"+this.checkAllInfo.length+"条维护记录，确认结束维护？\n")){
        let oids = "";
        for(let i=0;i<this.checkAllInfo.length;i++){
          oids += this.checkAllInfo[i]+";";
        }
        if(oids){
          this.maintainService.endMaintainRecords(oids).then(res=>{
              alert(res.message);
          });
        }
        this.checkAllInfo = [];
        this._checkInfo = [];
        this.refreshData();
      }
    }else{
      alert("请至少选择一条维护记录！\n");
    }
  }

  endrecord(oid){
    if(confirm("确认结束维护？")){
      this.maintainService.endMaintainRecord(oid);
    }
    this.maintainService.getRecordEvent.emit(["list"]);
  }

  updaterecord(oid){
    this.maintainService.getRecordEvent.emit(["edit",oid]);
  }

  viewrecord(oid){
    this.maintainService.getRecordEvent.emit(["view",oid]);
  }
}
